/**
 * Copyright 2008 blacky
 * The module below register HTML firefox convertor to remove JS code mistakes 
 * in files from combats.com domain.
 */
 
var com_combats = {}; // namespace declaration

/**
 * Set of constats
 */
com_combats.Properties = {
	domain: 'combats.com'
};

/**
 * Core object - contains method and properties of
 * base patch functionality that just enough for
 * playing in combats.com game.
 */
com_combats.Core = function()
{
  var aConsoleService;
  var aPrefService;
  var aObserverService;

  var debugMsg = function(msg) {
    msg = 'Combats Core:: ' + msg;
    if(aPrefService.getBoolPref('showInConsole'))
    {
      aConsoleService.logStringMessage(msg);
    }
    dump(msg);
  }

  var windowOnLoadHandler = function(e)
  {
    //debugMsg('OnLoad enter');
    //debugMsg('OnLoad leave');
  };
  
  var toStr = function(o)
  {
    if (!o)
		return 'null';
	return o;
  };
  
  var hostCheckRegexp = /^\w+\.combats\.com$/g;
  
  var httpEventsHandler =
  {
    observe: function(subject, topic, data) 
    {
      if (!subject)
      {
        debugMsg('Null response object was passed into handler. Subject: ' + toStr(subject) + '. Topic: ' + toStr(topic) + '. Data: ' + toStr(data));
      }
      var channel = subject.QueryInterface(Components.interfaces.nsIChannel);
      if (!channel)
      {
        debugMsg('Response object doesn\'t suppor nsIChannel interface');
        return;
      }
      if (!channel.URI)
      {
        debugMsg('Response object URI property is null');
        return;
      }
      var uri = channel.URI.QueryInterface(Components.interfaces.nsIURI);
      if (!uri)
      {
        debugMsg('URI property doesn\'t support nsIURI interface');
        return;
      }
      if (uri.host && uri.host.match(hostCheckRegexp) && uri.host !== 'img.combats.com' && 
          uri.host !== 'img.combats.ru' &&
          (uri.scheme === 'http' || uri.scheme === 'https') &&
          channel.contentType === 'text/html')
      {
         debugMsg('Observe enter. Topic: ' + toStr(topic) + '. URI: ' + toStr(uri.spec) + '. Content type: ' + toStr(uri.contentType));
         // the trick that make browser user combats convertor to get html content
         channel.contentType = 'text/com.combats.html';
         var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
         if (!httpChannel)
         {
			debugMsg('Response object doesn\'t support nsIHttpChannel interface');
         }
         httpChannel.setResponseHeader("Cache-Control", "no-cache", false);
         //debugMsg('Content type set');
         //debugMsg('Observe leave');
      }
	
    },
    QueryInterface: function(iid)
    { 
      if (!iid.equals(Components.interfaces.nsIObserver)) 
        throw Components.results.NS_ERROR_NO_INTERFACE; 
      return this; 
    }      
  };
  
  return {
	init: function()
	{
	  // init debug logging
      aConsoleService = Components.classes["@mozilla.org/consoleservice;1"].
        getService(Components.interfaces.nsIConsoleService);

      aPrefService = Components.classes["@mozilla.org/preferences-service;1"].
        getService(Components.interfaces.nsIPrefService).getBranch('extensions.uriid.'); 
     
      try
      {
        var js = aPrefService.getBoolPref('showInConsole');
      }
      catch(e)
      {
        aPrefService.setBoolPref('showInConsole', true);
      }
      
      // init http notification handlers
      aObserverService = Components.classes["@mozilla.org/observer-service;1"]
        .getService(Components.interfaces.nsIObserverService);

      //aObserverService.addObserver(httpEventsHandler, "http-on-modify-request", false);
      aObserverService.addObserver(httpEventsHandler, "http-on-examine-response", false);    
      aObserverService.addObserver(httpEventsHandler, "http-on-examine-merged-response", false);
      
      // test load even to check that initialization finished completelly
	  window.addEventListener("load", windowOnLoadHandler, false);	    
	  
	  
	}
  };
}();

com_combats.Core.init();